/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.cbkst2.prereq;

import java.util.Collection;

import javax.annotation.Nonnull;

import at.tugraz.kmi.css.cbkst2.CbKSTCompetence;
import at.tugraz.kmi.css.cbkst2.ImmutablePrerequesiteOrderSet;
import at.tugraz.kmi.css.cbkst2.PrerequesiteOrderSet;
import at.tugraz.kmi.css.cbkst2.PrerequesiteRelation;

import com.google.common.collect.ImmutableSet;

/**
 * Adapter from a Graph to a {@link PrerequesiteOrderSet}
 * 
 * @author Georg Öttl
 * 
 */
public final class PrerequesiteOrderSetAdapter implements
		PrerequesiteOrderSet<CbKSTCompetence> {

	private PrerequesiteOrderSetGraph<CbKSTCompetence, PrerequesiteRelation>	jungGraph;

	/**
	 * @param graph
	 *            The graph to Adapt.
	 */
	public PrerequesiteOrderSetAdapter(
			PrerequesiteOrderSetGraph<CbKSTCompetence, PrerequesiteRelation> graph) {
		jungGraph = graph;
	}

	/**
	 * 
	 * Set the Jung graph implementation to be used by this adapter.
	 * 
	 * @param jungGraph
	 *            The PrerequesiteOrderSetGraph to use.
	 */
	public void setJungGraph(
			PrerequesiteOrderSetGraph<CbKSTCompetence, PrerequesiteRelation> jungGraph) {
		this.jungGraph = jungGraph;
	}

	/**
	 * Get the jung graph currently used in this adapter.
	 * 
	 * @return The PrerequesiteOrderSetGraph implementation in use.
	 */
	public PrerequesiteOrderSetGraph<CbKSTCompetence, PrerequesiteRelation> getJungGraph() {
		return jungGraph;
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public boolean add(CbKSTCompetence sourceVertex) {
		return jungGraph.addVertex(sourceVertex);
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public boolean addPrerequesite(@Nonnull PrerequesiteRelation prereq) {
		return jungGraph.addEdge(prereq, prereq.getAncestor(),
				prereq.getDescendant());
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public boolean contains(@Nonnull CbKSTCompetence vertex) {
		return jungGraph.containsVertex(vertex);
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public boolean containsPrerequesite(@Nonnull PrerequesiteRelation prereq) {
		return jungGraph.findEdge(prereq.getAncestor(), prereq.getDescendant()) != null;
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public int size() {
		return jungGraph.getVertexCount();
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public boolean removePrerequesite(@Nonnull PrerequesiteRelation prereq) {
		return jungGraph.removeEdge(jungGraph.findEdge(prereq.getAncestor(),
				prereq.getDescendant()));
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public ImmutableSet<CbKSTCompetence> getCbKSTVertices() {
		return ImmutableSet.copyOf(jungGraph.getVertices());
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public ImmutableSet<PrerequesiteRelation> getPrerequisites() {
		return ImmutableSet.copyOf(jungGraph.getEdges());
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public ImmutableSet<CbKSTCompetence> getPrerequisites(CbKSTCompetence vertex) {
		return ImmutableSet.copyOf(jungGraph.getPredecessors(vertex));
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public ImmutableSet<CbKSTCompetence> getDescendants(CbKSTCompetence vertex) {
		Collection<CbKSTCompetence> successors = jungGraph
				.getSuccessors(vertex);
		if (successors == null) {
			return ImmutableSet.of();
		} else {
			return ImmutableSet.copyOf(successors);
		}
	}

	/**
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public void addAll(ImmutablePrerequesiteOrderSet<CbKSTCompetence> set) {
		jungGraph.addAll(set);
	}

	@Override
	public CbKSTCompetence find(String id) {

		return jungGraph.find(id);
	}

}
